﻿using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.City6473.Web.Data.Repositories;
using SpaceNeedle.SDK.Cloud.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SpaceNeedle.City6473.WebSite.Controllers
{
    public class AppContentsController : ApiController
    {
        public const string ContainerName = "content";
        public const string FileName = "AppContent.json";

        public AppContent Get(string id)
        {
            try
            {
                var blobClient = AzureStorage.Account.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(FileName);

                string content = blockBlob.DownloadText(System.Text.Encoding.Unicode);

                return JsonConvert.DeserializeObject<AppContent>(content);
            }
            catch { }

            return null;
        }

        // GET api/<controller>/5
        //public string Get(int id)
        //{
        //    return "value";
        //}

        // POST api/<controller>
        public void Post([FromBody]string value)
        {

        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {

        }

        public void Put(AppContent model)
        {
            try
            {
                var blobClient = AzureStorage.Account.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(FileName);

                blockBlob.UploadText(JsonConvert.SerializeObject(model), System.Text.Encoding.Unicode);
            }
            catch { }
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {

        }
    }
}